package com.base.service.system.controller;

import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.base.core.context.annotation.AuthenticationCheck;
import com.base.core.context.annotation.RestfulCheck;
import com.base.core.context.mvc.BaseV1Controller;
import com.base.service.system.ao.UserAO;
import com.base.service.system.ao.UserLoginAO;
import com.base.service.system.ao.UserPageAO;
import com.base.service.system.ao.UserPwdAO;
import com.base.service.system.ao.UserUpdateAO;
import com.base.service.system.dto.JwtUserDTO;
import com.base.service.system.service.UserService;
import com.base.service.system.service.impl.UserServiceImpl;
import com.base.service.system.vo.AuthVO;
import com.base.service.system.vo.UserLoginVO;
import com.base.service.system.vo.UserVO;
import com.gitee.magic.framework.head.vo.BaseVO;
import com.gitee.magic.framework.head.vo.ListVO;
import com.gitee.magic.framework.head.vo.ObjectVO;
import com.gitee.magic.framework.head.vo.PageVO;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

/**
 * @author start
 */
@Tag(name = "用户管理")
@RestController
public class UserController extends BaseV1Controller {

	@Resource
	private UserService userService;

	@RestfulCheck
	@Operation(summary="登录")
	@PostMapping("/v1/user/login")
	public ObjectVO<UserLoginVO> login(@RequestBody UserLoginAO ao) {
		return response(userService.login(ao,request.getRemoteAddr()));
	}
	
	@RestfulCheck
	@Operation(summary="登录Web")
	@PostMapping("/v1/user/loginweb")
	public ObjectVO<UserLoginVO> loginweb(@RequestBody UserLoginAO ao) {
		UserLoginVO uLoginVo=userService.login(ao,request.getRemoteAddr());
		request.getSession().setAttribute(UserServiceImpl.USER, uLoginVo.getToken());
		return response(uLoginVo);
	}

	@AuthenticationCheck
	@Operation(summary="创建用户")
	@PostMapping("/v1/user")
	public BaseVO save(@RequestBody UserAO ao) {
		userService.checkUserNameExist(ao.getUserName());
		userService.save(ao);
		return response();
	}

	@AuthenticationCheck
	@Operation(summary="用户更新")
	@PutMapping("/v1/user")
	public BaseVO update(@RequestBody UserUpdateAO ao) {
		userService.saveForUpdate(ao.getId(), ao);
		return response();
	}

	@AuthenticationCheck
	@Operation(summary="修改密码")
	@PutMapping("/v1/user/password")
	public BaseVO password(@RequestBody UserPwdAO ao) {
		JwtUserDTO jwt = requestCache(JwtUserDTO.class);
		userService.setPassword(ao,jwt.getUser().getId());
		return response();
	}
	
	@Operation(summary="重置MFA")
    @AuthenticationCheck
    @PutMapping("/v1/user/rest/mfa/{userId}")
    public ObjectVO<String> restMfa(
    		@PathVariable @Parameter(description = "用户Id") Long userId) {
    	return response(userService.restMfa(userId));
    }

	@AuthenticationCheck
	@Operation(summary="删除用户")
	@DeleteMapping("/v1/user")
	public ObjectVO<Integer> remove(@RequestBody List<Long> ao) {
		return response(userService.remove(ao));
	}

	@AuthenticationCheck
	@Operation(summary="用户分页")
	@PostMapping("/v1/user/page")
	public PageVO<UserVO> page(@RequestBody UserPageAO ao) {
		return response(userService.page(ao));
	}

	@AuthenticationCheck
	@Operation(summary="用户拥有权限")
	@GetMapping("/v1/user/auth/tree")
	public ListVO<AuthVO> userAuthTree() {
		JwtUserDTO jwt = requestCache(JwtUserDTO.class);
		return response(userService.userAuthTree(0L,jwt.getUser().getId()));
	}

}
